{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1a0cf63",
   "metadata": {},
   "source": [
    "# Predict VoxCeleb utterance info from SSL representations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abdc0067",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "import sys\n",
    "os.chdir('../..')\n",
    "sys.path.insert(1, os.path.join(sys.path[0], '../..'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a1bca02",
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebooks.notebooks_utils import (\n",
    "    load_models,\n",
    "    evaluate_models,\n",
    "    create_metrics_df\n",
    ")\n",
    "\n",
    "from sslsv.evaluations.CosineSVEvaluation import CosineSVEvaluation, CosineSVEvaluationTaskConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "914d6061",
   "metadata": {},
   "outputs": [],
   "source": [
    "models = load_models(\n",
    "    [\n",
    "        './models/old/vox2_ddp_sntxent_s=30_m=0/config.yml',\n",
    "        './models/old/vox2_ddp_sntxent_s=30_m=0.1/config.yml'\n",
    "    ],\n",
    "    override_names={\n",
    "        'models/old/vox2_ddp_sntxent_s=30_m=0'   : 'simclr',\n",
    "        'models/old/vox2_ddp_sntxent_s=30_m=0.1' : 'simclr_am'\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ac71a7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluate_models(models, CosineSVEvaluation, CosineSVEvaluationTaskConfig())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2866696",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "def fit_mlp_on_representations(model, y_key_pos):\n",
    "    keys = list(model['embeddings'].keys())\n",
    "    \n",
    "    X = [model['embeddings'][key][0].numpy() for key in keys]\n",
    "    if y_key_pos is None:\n",
    "        y = keys\n",
    "    else:\n",
    "        y = [key.split('/')[y_key_pos] for key in keys]\n",
    "    \n",
    "    clf = LogisticRegression()\n",
    "    clf.fit(X, y)\n",
    "    \n",
    "    print(f'Accuracy: {clf.score(X, y)}')\n",
    "    \n",
    "    return clf, X, y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "121e3351",
   "metadata": {},
   "source": [
    "## Speaker ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6486fa76",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr'], y_key_pos=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3773cbbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr_am'], y_key_pos=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53206004",
   "metadata": {},
   "source": [
    "## Video ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01cb149d",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr'], y_key_pos=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d45f290",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr_am'], y_key_pos=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "486ca144",
   "metadata": {},
   "source": [
    "## Segment ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d0574c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr'], y_key_pos=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6465e297",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr_am'], y_key_pos=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89a4b9bc",
   "metadata": {},
   "source": [
    "## Sample ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b5d09fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr'], y_key_pos=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b188fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = fit_mlp_on_representations(models['simclr_am'], y_key_pos=None)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
